package com.ttms.dao.impl;


import com.ttms.dao.StudioDao;
import com.ttms.pojo.Studio;

import java.util.List;

public class StudioDaoImpl extends BaseDao implements StudioDao {


    @Override
    public int addStudio(Studio studio) {
//         `studio_id` ,`studio_name` ,`studio_row_count` ,`studio_col_count` ,`studio_introduction` ,`studio_flag`
        String sql = "insert into studio(`studio_name` ,`studio_row_count` ,`studio_col_count` ,`studio_introduction` ,`studio_flag` ) values(?,?,?,?,?)";
        return update(sql, studio.getStudio_name(),studio.getStudio_row_count(),studio.getStudio_col_count(),studio.getStudio_introduction(),studio.getStudio_flag());
    }

    @Override
    public int deleteStudioById(Integer id) {
        String sql = "delete from studio where studio_id = ?";
        return update(sql, id);
    }

    @Override
    public int updateStudio(Studio studio) {
        String sql = "update studio set `studio_name`=? ,`studio_row_count`=? ,`studio_col_count`=? ,`studio_introduction`=? ,`studio_flag`=? where studio_id = ?";
        return update(sql, studio.getStudio_name(),studio.getStudio_row_count(),studio.getStudio_col_count(),studio.getStudio_introduction(),studio.getStudio_flag(),studio.getStudio_id());
    }

    @Override
    public Studio queryStudioById(Integer id) {
        String sql = "select `studio_id` ,`studio_name` ,`studio_row_count` ,`studio_col_count` ,`studio_introduction` ,`studio_flag` from studio where studio_id = ?";
        return queryForOne(Studio.class, sql,id);
    }

    @Override
    public List<Studio> queryStudios() {
        String sql = "select `studio_id` ,`studio_name` ,`studio_row_count` ,`studio_col_count` ,`studio_introduction` ,`studio_flag` from studio";
        return queryForList(Studio.class, sql);
    }

    @Override
    public Integer queryForPageTotalCount() {
        String sql = "select count(*) from studio";
        Number count = (Number) queryForSingleValue(sql);
        return count.intValue();
    }

    @Override
    public List<Studio> queryForPageItems(int begin, int pageSize) {
        String sql = "select `studio_id`,`studio_name`,  `studio_row_count`,  `studio_col_count` ,  `studio_introduction`,`studio_flag` from studio limit ?,?";
        return queryForList(Studio.class,sql,begin,pageSize);
    }

    @Override
    public List<Studio> queryStudioByName(int begin, int pageSize, String name) {
        String sql = "select * from studio where studio_name like '%"+ name +"%' order by studio_id limit ?,?";
        return queryForList(Studio.class,sql,begin,pageSize);
    }

    @Override
    public Integer queryStudioCountByName(String studio_name) {
        String sql = "select count(*) from studio where studio_name like '%"+ studio_name +"%' ";
        Number count = (Number) queryForSingleValue(sql);
        return count.intValue();
    }

    @Override
    public List<Studio> queryStudioByFlag() {
        String sql = "select * from studio where studio_flag = 1";
        return queryForList(Studio.class, sql);
    }
}
